import pymysql
conn = pymysql.connect(host="47.100.16.211", port=3306, user="root",password="Yjz81808261",database="cartoon",charset="utf8")

# 获取所有漫画列表
getAllCartoonsSql = "select * from t_cartoon";
# 获取漫画的最后一条记录
getLstestOfCartoonSql = "select * from t_chapter where cartoon_id = '%s' order by chapter_index desc limit 1";
# 插入漫画介绍页
addNewCartoonSql = "INSERT INTO `t_cartoon` (`cartoon_id`, `cartoon_name`, `cartoon_author`,  `album_pic`, `cartoon_resum`) " \
                 "VALUES (%s, %s, %s, %s, %s)"
# 插入章节内容
addNewChapterSql = "INSERT INTO `t_chapter` (`chapter_id`, `cartoon_id`, `chapter_index`, `chapter_name`, `chapter_content`, `chapter_cost`) " \
                "VALUES (%s, %s, %s, %s, %s, %s)"


# 获取所有漫画列表
def getAllCartoons():
    cursor = conn.cursor()
    cursor.execute(getAllCartoonsSql)
    result = cursor.fetchall()
    cursor.close()
    return result

# 获取漫画的最后一条记录
def getLastestChapterOfCartoon(cartoonId):
    cursor = conn.cursor()
    cursor.execute(getLstestOfCartoonSql %cartoonId)
    result = cursor.fetchone()
    cursor.close()
    return result


# 插入漫画介绍页
def addNewCartoon(cartoon):
    cursor = conn.cursor()
    # 执行SQL语句
    result = cursor.execute(addNewCartoonSql, [cartoon[0], cartoon[4], cartoon[5], cartoon[6], cartoon[8]])
    # 提交事务
    conn.commit()
    cursor.close()
    return result;

# 插入章节内容
def addNewChapter(chapter):
    cursor = conn.cursor()
    # 执行SQL语句
    chapter_cost = 0;
    if chapter[3] > 9:
        chapter_cost = 50;
    result = cursor.execute(addNewChapterSql, [chapter[1], chapter[0], chapter[3], chapter[2], '["'+'","'.join(chapter[4].split(",")) + '"]', chapter_cost])
    # 提交事务
    conn.commit()
    cursor.close()
    return result;